import axios from 'axios'
import { ElMessage, ElLoading as Loading } from 'element-plus'
const ConfigBaseURL = '/api' //默认路径，这里也可以使用env来判断环境
let loadingInstance:any = null //这里是loading
//使用create方法创建axios实例
export const request = axios.create({
    timeout: 5000, // 请求超时时间
    baseURL: ConfigBaseURL,
    method: 'post',
    headers: {
        'Content-Type': 'application/json;charset=UTF-8'
    }
})

// 添加请求拦截器
request.interceptors.request.use(config => {
    loadingInstance = Loading.service({
        lock: true,
        text: 'loading...'
    })
    return config
})
// 添加响应拦截器
request.interceptors.response.use(response => {
    loadingInstance.close()
    console.log(response);
    if(response.data.code != 0) ElMessage.error(`请求异常:${response.data.msg}`)
    return response.data
}, error => {
    console.log('TCL: error', error)
    const msg = error.Message !== undefined ? error.Message : '';
    ElMessage({
        message: '网络错误' + msg,
        type: 'error',
        duration: 3 * 1000
    })
    loadingInstance.close()
    return Promise.reject(error)
})

export default request